package com.qifenqian.turbot.module.ui.setting

import android.content.Context
import android.content.Intent
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.Constant.SP_ACCOUNT
import com.qifenqian.turbot.config.Constant.SP_KEY_USER_CODE
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.util.SendSmsUtil
import com.qifenqian.turbot.util.SpUtils
import com.qifenqian.turbot.util.StringUtils
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_modify_phone.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/29 8:52
 * @description 修改手机号
 * @see
 * @since
 */
class ModifyPhoneActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, ModifyPhoneActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_modify_phone
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

    private var mCurrentPhone = "15656566565"//Constant.userCode
    override fun initial() {
        mCurrentPhone = mCurrentPhone.replace(Regex("(\\d{3})\\d{4}(\\d{4})"), "$1****$2")
        tv_modify_phone_current.text = mCurrentPhone
        btn_modify_phone_get_code.setOnClickListener {
            if (et_modify_phone_new_phone.text.toString().isBlank()) {
                toast("请输入手机号")
                return@setOnClickListener
            }
            if (!StringUtils.isPhoneNumber(et_modify_phone_new_phone.text.toString())) {
                toast("手机号码格式错误")
                return@setOnClickListener
            }
            mPresenter!!.sendSmsCode(TurbotEnum.SMS_MODIFY_PHONE.type)
        }
        btn_modify_phone_next.setOnClickListener {
            if (et_modify_phone_password.text.toString().isEmpty()) {
                toast("请输入密码")
                return@setOnClickListener
            }
            if (et_modify_phone_new_phone.text.toString().isEmpty()) {
                toast("请输入手机号码")
                return@setOnClickListener
            }
            if (et_modify_phone_sms.text.toString().isEmpty()) {
                toast("请输入验证码")
                return@setOnClickListener
            }
            mPresenter!!.modifyPhone(SpUtils.getInstance().getString(SP_KEY_USER_CODE, SP_ACCOUNT), et_modify_phone_password.text.toString(), et_modify_phone_new_phone.text.toString(), et_modify_phone_sms.text.toString())
        }
    }

    override fun sendSmsCodeResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            SendSmsUtil.beginCountdown(this, Constant.COUNTDOWN_TIME, btn_login_get_code)
        } else {
            SendSmsUtil.disposeCountdown()
        }
        toast("发送成功")
    }

    override fun modifyPhoneResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            SpUtils.getInstance().putString(Constant.SP_KEY_USER_CODE, et_modify_phone_new_phone.text.toString(), Constant.SP_ACCOUNT)
            finish()
        }
        toast(msg!!)
    }
}
